<template>
    <div id="app">
        <h1>list</h1>
        <h2 id="list-summary">{{listSummary}}</h2>
        <!--添加事件监听的todo-added -->
        <ToDoForm @added="addToDo"></ToDoForm>
        <ToDoItem v-for="item in ToDoItems" :id="item.id" :label="item.label" :done="item.done"
        @checkbox-changed="updateDoneStatus(item.id)"></ToDoItem>
    </div>
</template>
<script>
import uniqueId from 'lodash.uniqueid';
import ToDoForm from './components/ToDoForm.vue';
import ToDoItem from './components/ThreeItem.vue';

export default {
   name:'app',
   components:{
       ToDoForm,
       ToDoItem
   },
   computed: {
  listSummary() {
    const numberFinishedItems = this.ToDoItems.filter(item =>item.done).length
    return `${numberFinishedItems} out of ${this.ToDoItems.length} items completed`
  }
},

   data(){
      return {
        ToDoItems:[
          {id:uniqueId('todo-'),label: 'Learn Vue',done:true },
          {id:uniqueId('todo-'), label: 'Learn Vue',done:false}
        ]
      };
   },
   
   methods:{
    //    你看到的任何文本输入记录在你的控制台提交.vue提交后的事件中的名称this.$emit()对事件的处理程序
       addToDo(toDoLabel){
           this.ToDoItems.push({id:uniqueId('todo-'),label: toDoLabel,done:false});
           
           console.log('To-do added',toDoLabel)
       },
      // 传递待办项目id,找到该项目的匹配id和
      // 更新它的done状态对应其情况
       updateDoneStatus(toDoId) {
        const toDoToUpdate = this.ToDoItems.find(item => item.id === toDoId)
        toDoToUpdate.done = !toDoToUpdate.done
      }
   }
  

}
</script>
<style>
/* Global styles */
.btn {
  padding: 0.8rem 1rem 0.7rem;
  border: 0.2rem solid #4d4d4d;
  cursor: pointer;
  text-transform: capitalize;
}
.btn__danger {
  color: #fff;
  background-color: #ca3c3c;
  border-color: #bd2130;
}
.btn__filter {
  border-color: lightgrey;
}
.btn__danger:focus {
  outline-color: #c82333;
}
.btn__primary {
  color: #fff;
  background-color: #000;
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
.btn-group > * {
  flex: 1 1 auto;
}
.btn-group > * + * {
  margin-left: 0.8rem;
}
.label-wrapper {
  margin: 0;
  flex: 0 0 100%;
  text-align: center;
}
[class*="__lg"] {
  display: inline-block;
  width: 100%;
  font-size: 1.9rem;
}
[class*="__lg"]:not(:last-child) {
  margin-bottom: 1rem;
}
@media screen and (min-width: 620px) {
  [class*="__lg"] {
    font-size: 2.4rem;
  }
}
.visually-hidden {
  position: absolute;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip: rect(1px 1px 1px 1px);
  clip: rect(1px, 1px, 1px, 1px);
  clip-path: rect(1px, 1px, 1px, 1px);
  white-space: nowrap;
}
[class*="stack"] > * {
  margin-top: 0;
  margin-bottom: 0;
}
.stack-small > * + * {
  margin-top: 1.25rem;
}
.stack-large > * + * {
  margin-top: 2.5rem;
}
@media screen and (min-width: 550px) {
  .stack-small > * + * {
    margin-top: 1.4rem;
  }
  .stack-large > * + * {
    margin-top: 2.8rem;
  }
}
/* End global styles */
#app {
  background: #fff;
  margin: 2rem 0 4rem 0;
  padding: 1rem;
  padding-top: 0;
  position: relative;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 2.5rem 5rem 0 rgba(0, 0, 0, 0.1);
}
@media screen and (min-width: 550px) {
  #app {
    padding: 4rem;
  }
}
#app > * {
  max-width: 50rem;
  margin-left: auto;
  margin-right: auto;
}
#app > form {
  max-width: 100%;
}
#app h1 {
  display: block;
  min-width: 100%;
  width: 100%;
  text-align: center;
  margin: 0;
  margin-bottom: 1rem;
}
</style>